<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="房产类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请输入房产类型">
          <el-option value="别墅" label="别墅"></el-option>
          <el-option value="楼房" label="楼房"></el-option>
        </el-select>
      </el-form-item>
          <el-form-item label="房屋面积" prop="area">
          <el-input v-model="dataForm.area" placeholder="房屋面积"></el-input>
      </el-form-item>
      <el-form-item label="生效日期" prop="houseTime">
        <div class="block">
          <span class="demonstration"></span>
          <el-date-picker
              v-model="dataForm.houseTime"
              type="date"
              value-format="yyyy-MM-dd hh:mm:ss"
              placeholder="生效日期">
          </el-date-picker>
        </div>
      </el-form-item>
          <el-form-item label="单元号" prop="unit">
          <el-input v-model="dataForm.unit" placeholder="单元号"></el-input>
      </el-form-item>
          <el-form-item label="更新者" prop="updator">
          <el-input v-model="dataForm.updator" placeholder="更新者"></el-input>
      </el-form-item>
      <el-form-item label="楼房编号" prop="buildingId">
        <el-select v-model="dataForm.buildingId" placeholder="请选择小区名称">
          <el-option
              v-for="building in allbuilding"
              :key="building.id"
              :label="building.buildingName"
              :value="building.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      allbuilding: '',
      dataForm: {
        id: '',
        type: '',
        area: '',
        houseTime: '',
        unit: '',
        creator: '',
        createTime: '',
        updator: '',
        updateTime: '',
        icon: '',
        buildingId: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        area: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        houseTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unit: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updator: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getAllBuilding();
  },
  methods: {
    getAllBuilding() {
      this.$http.get(`/eightapp/eibuildinginfo/`).then(({data: res}) => {
        console.log(res)
        this.allbuilding = res.data
        console.log(this.allbuilding)
      }).catch(() => {
      })

    },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/eightapp/eihouseinfo/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/eightapp/eihouseinfo/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
